package com.coocaa.aries.codegen.handler.impl;

import com.coocaa.aries.codegen.constant.TypeConstants;
import com.coocaa.aries.codegen.convert.ClassNameConvert;
import com.coocaa.aries.codegen.model.CodeGenConfigModel;
import com.coocaa.aries.codegen.model.TableConfigInfo;
import com.coocaa.aries.codegen.model.TableInfoModel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @description
 * @date 2020/1/19 10:14
 */
public class VirgoCodeGenHandlerImpl extends BaseCodeGenHandlerImpl {

    @Override
    protected boolean isCreate(String filePath, TableConfigInfo tableConfigInfo) {
        File file = new File(filePath);
        Integer fileOverride = tableConfigInfo.getFileOverride();
        boolean isCanWrite = !file.exists() || (fileOverride != null && fileOverride == 1);
        return isCanWrite;
    }

    @Override
    protected List<String> getTypes(String type) {
        List<String> types = new ArrayList<>(2);
        if (TypeConstants.ENTITY.equals(type)) {
            types.add(TypeConstants.ENTITY);
            types.add(TypeConstants.HISTORY_TABLE_ENTITY);
        }else if (TypeConstants.MAPPER.equals(type)) {
            types.add(TypeConstants.MAPPER);
            types.add(TypeConstants.HISTORY_MAPPER);
        } else if (TypeConstants.XML.equals(type)) {
            types.add(TypeConstants.XML);
            types.add(TypeConstants.HISTORY_XML);
        } else if (TypeConstants.PROVIDER.equals(type)) {
            //有查询和更新的provider接口
            types.add(TypeConstants.QUERY_PROVIDER);
            types.add(TypeConstants.UPDATE_PROVIDER);
            types.add(TypeConstants.RLS_UPDATE_PROVIDER);
        } else if (TypeConstants.PROVIDER_IMPL.equals(type)) {
            //有查询和更新的provider接口实现
            types.add(TypeConstants.QUERY_PROVIDER_IMPL);
            types.add(TypeConstants.UPDATE_PROVIDER_IMPL);
            types.add(TypeConstants.AUDIT_TASK_PROVIDER_IMPL);
            types.add(TypeConstants.RLS_UPDATE_PROVIDER_IMPL);
        } else if (TypeConstants.SERVICE.equals(type)) {
            //有查询和更新的service接口
            types.add(TypeConstants.QUERY_SERVICE);
            types.add(TypeConstants.UPDATE_SERVICE);
        } else if (TypeConstants.SERVICE_IMPL.equals(type)) {
            //有查询和更新的service接口实现
            types.add(TypeConstants.QUERY_SERVICE_IMPL);
            types.add(TypeConstants.UPDATE_SERVICE_IMPL);
        } else {
            types.add(type);
        }
        return types;

    }

    @Override
    protected Map<String, String> getPackages(TableInfoModel tableInfo, CodeGenConfigModel model) {
        return super.getPackages(tableInfo, model);
    }

    @Override
    protected Map<String, String> getNames(String tableName2ClassName, CodeGenConfigModel model) {
        Map<String, String> data = super.getNames(tableName2ClassName, model);
        data.put(TypeConstants.AUDIT_TASK_PROVIDER_IMPL, ClassNameConvert.convert2AuditTaskProviderImplName(tableName2ClassName, model.getClassNameConfig()));
        data.put(TypeConstants.RLS_UPDATE_PROVIDER, ClassNameConvert.convert2RlsUpdateProviderName(tableName2ClassName, model.getClassNameConfig()));
        data.put(TypeConstants.RLS_UPDATE_PROVIDER_IMPL, ClassNameConvert.convert2RlsUpdateProviderImplName(tableName2ClassName, model.getClassNameConfig()));
        data.put(TypeConstants.HISTORY_MAPPER, ClassNameConvert.convert2HistoryMapperName(tableName2ClassName, model.getClassNameConfig()));
        data.put(TypeConstants.HISTORY_XML, ClassNameConvert.convert2HistoryXmlName(tableName2ClassName, model.getClassNameConfig()));
        data.put(TypeConstants.HISTORY_TABLE_ENTITY, ClassNameConvert.convert2HistoryTableEntityName(tableName2ClassName, model.getClassNameConfig()));
        return data;
    }
}
